// 配置axios默认设置
axios.defaults.baseURL = '';
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';

new Vue({
    el: '#app',
    data: {
        loginForm: {
            userName: '',
            password: ''
        },
        errorMessage: '',
        isLoading: false,
        showPassword: false
    },
    methods: {
        handleLogin() {
            if (!this.loginForm.userName || !this.loginForm.password) {
                this.showError('用户名和密码不能为空');
                return;
            }

            this.isLoading = true;
            this.errorMessage = '';

            const params = new URLSearchParams();
            params.append('userName', this.loginForm.userName);
            params.append('password', this.loginForm.password);

            axios.post('login', params)
                .then(response => {
                    if (response.data.success) {
                        this.handleLoginSuccess();
                    } else {
                        this.showError(response.data.message || '用户名或密码错误');
                    }
                })
                .catch(error => {
                    console.error('Login error:', error);
                    this.showError('系统错误，请稍后重试');
                })
                .finally(() => {
                    setTimeout(() => {
                        this.isLoading = false;
                    }, 500);
                });
        },
        showError(message) {
            this.errorMessage = message;
            // 自动清除错误信息
            setTimeout(() => {
                this.errorMessage = '';
            }, 3000);
        },
        handleLoginSuccess() {
            // 添加登录成功的动画效果
            const loginBox = document.querySelector('.login-box');
            loginBox.style.transform = 'scale(0.95)';
            setTimeout(() => {
                loginBox.style.transform = 'scale(1.05)';
                setTimeout(() => {
                    loginBox.style.transform = 'scale(1)';
                    window.location.href = 'index.html';
                }, 100);
            }, 100);
        },
        togglePassword() {
            this.showPassword = !this.showPassword;
        },
        handleFocus(event) {
            event.target.parentElement.style.borderColor = '#0097e6';
        },
        handleBlur(event) {
            event.target.parentElement.style.borderColor = '#dfe6e9';
        }
    }
}); 